package com.yz.imagrun;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.Transition;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        imageView_main = (ImageView) findViewById(R.id.imageView_main);
    }

    public void clickView(View view) {

        switch (view.getId()) {
            case R.id.button_main:
                float screen = getResources().getDisplayMetrics().densityDpi;
                float xdp = getResources().getDisplayMetrics().widthPixels / screen/160.0f;
                float ydp = getResources().getDisplayMetrics().heightPixels / screen/160.0f;
                Animator animator= ObjectAnimator.ofFloat(imageView_main, "translateX",0,xdp-100.0f);
                animator= ObjectAnimator.ofFloat(imageView_main, "translateY",0,-(ydp-100.0f));
                animator= ObjectAnimator.ofFloat(imageView_main, "translateX",xdp-100.0f,0.0f);
                animator= ObjectAnimator.ofFloat(imageView_main, "translateY",-(ydp-100.0f),0.0f);
                animator.setDuration(1000);
                ((ObjectAnimator)animator).setRepeatMode(ValueAnimator.REVERSE);
                ((ObjectAnimator)animator).setRepeatCount(ValueAnimator.INFINITE);

                break;


        }
    }
}
